/****************************************************************************************
原作者：温子琪
修改者：李柯
修改日期：2017/08/28
修改内容：增加几种刷图算法
****************************************************************************************/
#include <stdio.h>   	//printf scanf
#include <fcntl.h>		//open write read lseek close  	 
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>

#include "lcd.h"
#include <math.h>
#if EN_LCD_SHOW_JPG
#include "jpeglib.h"
#endif

#include "api_v4l2.h"
#define PI 3.14159
static char g_color_buf[FB_SIZE]={0};

volatile int g_jpg_in_jpg_x;
volatile int g_jpg_in_jpg_y;


int lcd_init(void)
{
	lcd_fd = open("/dev/fb0", O_RDWR);
	
	if(lcd_fd<0)
	{
			printf("open lcd error\n");
			return -1;
	}

	lcd_mmap  = (int *)mmap(	NULL, 						//Ó³ÉäÇøµÄ¿ªÊ¼µØÖ·£¬ÉèÖÃÎªNULLÊ±±íÊ¾ÓÉÏµÍ³¾ö¶¨Ó³ÉäÇøµÄÆðÊ¼µØÖ·
									FB_SIZE, 				//Ó³ÉäÇøµÄ³¤¶È
									PROT_READ|PROT_WRITE, 	//ÄÚÈÝ¿ÉÒÔ±»¶ÁÈ¡ºÍÐ´Èë
									MAP_SHARED,				//¹²ÏíÄÚ´æ
									lcd_fd, 				//ÓÐÐ§µÄÎÄ¼þÃèÊö´Ê
									0						//±»Ó³Éä¶ÔÏóÄÚÈÝµÄÆðµã
								);

	return lcd_fd;

}

//LCD»­µã
void lcd_draw_point(unsigned int x,unsigned int y, unsigned int color)
{
	*(lcd_mmap+y*800+x)=color;
}

#if EN_LCD_SHOW_JPG
//坐标转换，参数为转化前坐标（dx, dy）,角度dAngle,转化后坐标(iNewx,iNewY),转化圆心(x,y)
void vRotationTransform(double dX, double dY, double dAngle, int *iNewX, int *iNewY,int x, int y)  
{   
	double x1 = dX - x;
	double y1 = dY -y;
	dAngle = dAngle/180*PI;
    *iNewX = x1 * cos(dAngle) - y1 * sin(dAngle)+x;  
    *iNewY = x1 * sin(dAngle) + y1 * cos(dAngle)+y;  
} 

//rate:缩放比例，rotate:旋转角度
int lcd_draw_jpg_circle(int x,int y,int halflen,const char *pjpg_path,
							char *pjpg_buf,unsigned int jpg_buf_size,unsigned int rate,int rotate)  
{
	/*定义解码对象，错误处理对象*/
	struct 	jpeg_decompress_struct 	cinfo;
	struct 	jpeg_error_mgr 			jerr;	
	
	char 	*pcolor_buf = g_color_buf;
	char 	*pjpg;
	
			 int 	i=0,j=0;
	unsigned int	color =0;
	unsigned int	count =0;
	
	unsigned int 	x_s = x;
	unsigned int 	x_e ;	
	unsigned int 	y_e ;
	
			 int	jpg_fd;
	unsigned int 	jpg_size;
	
	unsigned int 	jpg_width;
	unsigned int 	jpg_height;
	
	if(pjpg_path!=NULL)
	{
		/* 申请jpg资源，权限可读可写 */	
		jpg_fd=open(pjpg_path,O_RDWR);	
		if(jpg_fd == -1)
		{
		   printf("open %s error\n",pjpg_path);	   
		   return -1;	
		}		
		/* 获取jpg文件的大小 */
		jpg_size=file_size_get(pjpg_path);	
		/* 为jpg文件申请内存空间 */	
		pjpg = malloc(jpg_size);
		/* 读取jpg文件所有内容到内存 */		
		read(jpg_fd,pjpg,jpg_size);
	}
	else
	{
		jpg_size = jpg_buf_size;		
		pjpg = pjpg_buf;
	}
	/*注册出错处理*/
	cinfo.err = jpeg_std_error(&jerr);
	/*创建解码*/
	jpeg_create_decompress(&cinfo);
	/*直接解码内存数据*/		
	jpeg_mem_src(&cinfo,pjpg,jpg_size);	
	/*读文件头*/
	jpeg_read_header(&cinfo, TRUE);
	/*开始解码*/
	jpeg_start_decompress(&cinfo);	
	x_e	= x_s+cinfo.output_width;
	y_e	= y  +cinfo.output_height;	

	/*读解码数据*/
	
	int pic_buf[cinfo.output_width*cinfo.output_height];//保存图片信息
	memset(pic_buf,0,sizeof(pic_buf));
	while(cinfo.output_scanline < cinfo.output_height )
	{		
		pcolor_buf = g_color_buf;	
		/* 读取jpg一行的rgb值 */
		jpeg_read_scanlines(&cinfo,&pcolor_buf,1);			
		for(i=0; i<cinfo.output_width; i++)
		{
			/* 获取rgb值 */
			color = 		*(pcolor_buf+2);
			color = color | *(pcolor_buf+1)<<8;
			color = color | *(pcolor_buf)<<16;
			
			/* 显示像素点 */
			pic_buf[j*cinfo.output_width+i] = color;			
			pcolor_buf +=3;
		}
		j++;
	}

	int w = cinfo.output_width/rate;
	int h = cinfo.output_height/rate;
	
	int pic_buf_rate[w *h];//按比例进行转化后的图像像素数组
	int x2,y2;
	for(i=0; i<h; i++)
	{
		for(j=0; j<w; j++)
			pic_buf_rate[i*w +j] = pic_buf[i*rate*cinfo.output_width+j*rate];
	}
	
	
	int pic_buf_rotate[w *h];//旋转后图像像素数组
	for(i=0; i<h; i++)
	{
		for(j=0; j<w; j++)
		{		
			vRotationTransform((double)j, (double)i, (double)rotate, &x2, &y2,w/2,h/2);
			pic_buf_rotate[i*w +j] = pic_buf_rate[y2*w+x2];
		}
	}
	
	for(i=y-halflen; i<=y+halflen; i++)
	{
		j = x-halflen;
		while(j<x+halflen)
		{
			if((x-j)*(x-j) <= (halflen*halflen - (i-y)*(i-y)))
			{
				lcd_draw_point( j, i, pic_buf_rotate[(i-y+h/2)*w+j-x+w/2]);
			}
			j++;
		}
	}
	/*解码完成*/
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	if(pjpg_path!=NULL)
	{
		/* 关闭jpg文件 */
		close(jpg_fd);	
		
		/* 释放jpg文件内存空间 */
		free(pjpg);		
	}
	return 0;
}



int lcd_draw_jpg(unsigned int x,unsigned int y,const char *pjpg_path,char *pjpg_buf,unsigned int jpg_buf_size,unsigned int jpg_half)  
{
	/*¶¨Òå½âÂë¶ÔÏó£¬´íÎó´¦Àí¶ÔÏó*/
	struct 	jpeg_decompress_struct 	cinfo;
	struct 	jpeg_error_mgr 			jerr;	
	
	char 	*pcolor_buf = g_color_buf;
	char 	*pjpg;
	
	unsigned int 	i=0;
	unsigned int	color =0;
	unsigned int	count =0;
	
	unsigned int 	x_s = x;
	unsigned int 	x_e ;	
	unsigned int 	y_e ;
	
			 int	jpg_fd;
	unsigned int 	jpg_size;
	
	unsigned int 	jpg_width;
	unsigned int 	jpg_height;
	

	if(pjpg_path!=NULL)
	{
		/* ÉêÇëjpg×ÊÔ´£¬È¨ÏÞ¿É¶Á¿ÉÐ´ */	
		jpg_fd=open(pjpg_path,O_RDWR);
		
		if(jpg_fd == -1)
		{
		   printf("open %s error\n",pjpg_path);
		   
		   return -1;	
		}	
		
		/* »ñÈ¡jpgÎÄ¼þµÄ´óÐ¡ */
		jpg_size=file_size_get(pjpg_path);	

		/* ÎªjpgÎÄ¼þÉêÇëÄÚ´æ¿Õ¼ä */	
		pjpg = malloc(jpg_size);

		/* ¶ÁÈ¡jpgÎÄ¼þËùÓÐÄÚÈÝµ½ÄÚ´æ */		
		read(jpg_fd,pjpg,jpg_size);
	}
	else
	{
		jpg_size = jpg_buf_size;
		
		pjpg = pjpg_buf;
	}

	/*×¢²á³ö´í´¦Àí*/
	cinfo.err = jpeg_std_error(&jerr);

	/*´´½¨½âÂë*/
	jpeg_create_decompress(&cinfo);

	/*Ö±½Ó½âÂëÄÚ´æÊý¾Ý*/		
	jpeg_mem_src(&cinfo,pjpg,jpg_size);
	
	/*¶ÁÎÄ¼þÍ·*/
	jpeg_read_header(&cinfo, TRUE);

	/*¿ªÊ¼½âÂë*/
	jpeg_start_decompress(&cinfo);	
	
	
	if(jpg_half)
	{
		x_e	= x_s+(cinfo.output_width/2);
		y_e	= y  +(cinfo.output_height/2);		
		
		/*¶Á½âÂëÊý¾Ý*/
		while(cinfo.output_scanline < cinfo.output_height)
		{		
			pcolor_buf = g_color_buf;
			
			/* ¶ÁÈ¡jpgÒ»ÐÐµÄrgbÖµ */
			jpeg_read_scanlines(&cinfo,&pcolor_buf,1);			
			
			/* ÔÙ¶ÁÈ¡jpgÒ»ÐÐµÄrgbÖµ */
			jpeg_read_scanlines(&cinfo,&pcolor_buf,1);

			for(i=0; i<(cinfo.output_width/2); i++)
			{
				/* »ñÈ¡rgbÖµ */
				color = 		*(pcolor_buf+2);
				color = color | *(pcolor_buf+1)<<8;
				color = color | *(pcolor_buf)<<16;
				
				/* ÏÔÊ¾ÏñËØµã */
				lcd_draw_point(x,y,color);
				
				pcolor_buf +=6;
				
				x++;
			}
			
			/* »»ÐÐ */
			y++;					
			
			
			x = x_s;	

			
		}
	}
	else
	{
		x_e	= x_s+cinfo.output_width;
		y_e	= y  +cinfo.output_height;	

		/*¶Á½âÂëÊý¾Ý*/
		while(cinfo.output_scanline < cinfo.output_height )
		{		
			pcolor_buf = g_color_buf;
			
			/* ¶ÁÈ¡jpgÒ»ÐÐµÄrgbÖµ */
			jpeg_read_scanlines(&cinfo,&pcolor_buf,1);
			
			for(i=0; i<cinfo.output_width; i++)
			{
				/* »ñÈ¡rgbÖµ */
				color = 		*(pcolor_buf+2);
				color = color | *(pcolor_buf+1)<<8;
				color = color | *(pcolor_buf)<<16;
				
				/* ÏÔÊ¾ÏñËØµã */
				lcd_draw_point(x,y,color);
				
				pcolor_buf +=3;
				
				x++;
			}
			
			/* »»ÐÐ */
			y++;			
			
			x = x_s;
			
		}		
	}
	

				
	/*½âÂëÍê³É*/
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	if(pjpg_path!=NULL)
	{
		/* ¹Ø±ÕjpgÎÄ¼þ */
		close(jpg_fd);	
		
		/* ÊÍ·ÅjpgÎÄ¼þÄÚ´æ¿Õ¼ä */
		free(pjpg);		
	}
	return 0;
}

int lcd_draw_jpg_in_jpg(unsigned int x,unsigned int y,const char *pjpg_path,char *pjpg_buf,unsigned int jpg_buf_size)  
{
	/*¶¨Òå½âÂë¶ÔÏó£¬´íÎó´¦Àí¶ÔÏó*/
	struct 	jpeg_decompress_struct 	cinfo;
	struct 	jpeg_error_mgr 			jerr;	
	
	char 	*pcolor_buf = g_color_buf;
	char 	*pjpg;
	
	unsigned int 	i=0,j=0;
	unsigned int	color =0;
	unsigned int	count =0;
	
	unsigned int 	x_s = x;
	unsigned int 	x_e ;	
	unsigned int 	y_e ;
	unsigned int	y_n	= y;
	unsigned int	x_n	= x;
	
			 int	jpg_fd;
	unsigned int 	jpg_size;

	if(pjpg_path!=NULL)
	{
		/* ÉêÇëjpg×ÊÔ´£¬È¨ÏÞ¿É¶Á¿ÉÐ´ */	
		jpg_fd=open(pjpg_path,O_RDWR);
		
		if(jpg_fd == -1)
		{
		   printf("open %s error\n",pjpg_path);
		   
		   return -1;	
		}	
		
		/* »ñÈ¡jpgÎÄ¼þµÄ´óÐ¡ */
		jpg_size=file_size_get(pjpg_path);	

		/* ÎªjpgÎÄ¼þÉêÇëÄÚ´æ¿Õ¼ä */	
		pjpg = malloc(jpg_size);

		/* ¶ÁÈ¡jpgÎÄ¼þËùÓÐÄÚÈÝµ½ÄÚ´æ */		
		read(jpg_fd,pjpg,jpg_size);
	}
	else
	{
		jpg_size = jpg_buf_size;
		
		pjpg = pjpg_buf;
	}

	/*×¢²á³ö´í´¦Àí*/
	cinfo.err = jpeg_std_error(&jerr);

	/*´´½¨½âÂë*/
	jpeg_create_decompress(&cinfo);

	/*Ö±½Ó½âÂëÄÚ´æÊý¾Ý*/		
	jpeg_mem_src(&cinfo,pjpg,jpg_size);
	
	/*¶ÁÎÄ¼þÍ·*/
	jpeg_read_header(&cinfo, TRUE);

	/*¿ªÊ¼½âÂë*/
	jpeg_start_decompress(&cinfo);	
	
	
	x_e	= x_s+cinfo.output_width;
	y_e	= y  +cinfo.output_height;	

	/*¶Á½âÂëÊý¾Ý*/
	while(cinfo.output_scanline < cinfo.output_height )
	{		
		pcolor_buf = g_color_buf;
		
		/* ¶ÁÈ¡jpgÒ»ÐÐµÄrgbÖµ */
		jpeg_read_scanlines(&cinfo,&pcolor_buf,1);
		
		for(i=0; i<cinfo.output_width; i++)
		{
			/* ²»ÏÔÊ¾µÄ²¿·Ö */
			if(y_n>g_jpg_in_jpg_y && y_n<g_jpg_in_jpg_y+240)
				if(x_n>g_jpg_in_jpg_x && x_n<g_jpg_in_jpg_x+320)
				{
					pcolor_buf +=3;
				
					x_n++;
					
					continue;
				}
				
			/* »ñÈ¡rgbÖµ */
			color = 		*(pcolor_buf+2);
			color = color | *(pcolor_buf+1)<<8;
			color = color | *(pcolor_buf)<<16;	
			
			/* ÏÔÊ¾ÏñËØµã */
			lcd_draw_point(x_n,y_n,color);
			
			pcolor_buf +=3;
			
			x_n++;
		}
		
		/* »»ÐÐ */
		y_n++;			
		
		x_n = x_s;
		
	}		

				
	/*½âÂëÍê³É*/
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	if(pjpg_path!=NULL)
	{
		/* ¹Ø±ÕjpgÎÄ¼þ */
		close(jpg_fd);	
		
		/* ÊÍ·ÅjpgÎÄ¼þÄÚ´æ¿Õ¼ä */
		free(pjpg);		
	}


	
	return 0;
}
#endif

//LCDÈÎÒâµØÖ·»æÖÆÍ¼Æ¬
int lcd_draw_bmp(unsigned int x,unsigned int y,const char *pbmp_path)   
{
			 int bmp_fd;
	unsigned int blue, green, red;
	unsigned int color;
	unsigned int bmp_width;
	unsigned int bmp_height;
	unsigned int bmp_type;
	unsigned int bmp_size;
	unsigned int x_s = x;
	unsigned int x_e ;	
	unsigned int y_e ;
	unsigned char buf[54]={0};
			 char *pbmp_buf=g_color_buf;
	
	/* ÉêÇëÎ»Í¼×ÊÔ´£¬È¨ÏÞ¿É¶Á¿ÉÐ´ */	
	bmp_fd=open(pbmp_path,O_RDWR);
	
	if(bmp_fd == -1)
	{
	   printf("open bmp error\r\n");
	   
	   return -1;	
	}
	
	/* ¶ÁÈ¡Î»Í¼Í·²¿ÐÅÏ¢ */
	read(bmp_fd,buf,54);
	
	/* ¿í¶È  */
	bmp_width =buf[18];
	bmp_width|=buf[19]<<8;
	// printf("bmp_width=%d\r\n",bmp_width);
	
	/* ¸ß¶È  */
	bmp_height =buf[22];
	bmp_height|=buf[23]<<8;
	// printf("bmp_height=%d\r\n",bmp_height);	
	
	/* ÎÄ¼þÀàÐÍ */
	bmp_type =buf[28];
	bmp_type|=buf[29]<<8;
	// printf("bmp_type=%d\r\n",bmp_type);	

	/* ÉèÖÃÏÔÊ¾x¡¢y×ø±ê½áÊøÎ»ÖÃ */
	x_e = x + bmp_width;
	y_e = y + bmp_height;
	
	/* »ñÈ¡Î»Í¼ÎÄ¼þµÄ´óÐ¡ */
	bmp_size=file_size_get(pbmp_path);
	
	/* ¶ÁÈ¡ËùÓÐRGBÊý¾Ý */
	read(bmp_fd,pbmp_buf,bmp_size-54);
	
	for(;y < y_e; y++)
	{
		for (;x < x_e; x++)
		{
				/* »ñÈ¡ºìÂÌÀ¶ÑÕÉ«Êý¾Ý */
				blue  = *pbmp_buf++;
				green = *pbmp_buf++;
				red   = *pbmp_buf++;
				
				/* ÅÐ¶Ïµ±Ç°µÄÎ»Í¼ÊÇ·ñ32Î»ÑÕÉ« */
				if(bmp_type == 32)
				{
					pbmp_buf++;
				}
				
				/* ×é³É24bitÑÕÉ« */
				color = red << 16 | green << 8 | blue << 0;
				lcd_draw_point(x, 2*y_e-bmp_height-y-1, color);				
		}
		
		x = x_s;
	}
	
	/* ²»ÔÙÊ¹ÓÃBMP£¬ÔòÊÍ·Åbmp×ÊÔ´ */
	close(bmp_fd);	
	
	return 0;
}

//LCD¹Ø±Õ
void lcd_close(void)
{
	
	/* È¡ÏûÄÚ´æÓ³Éä */
	munmap(lcd_mmap, FB_SIZE);
	
	/* ¹Ø±ÕLCDÉè±¸ */
	close(lcd_fd);
}

void lcd_fill(unsigned int x_s,unsigned int y_s, 
              unsigned int x_e,unsigned int y_e,
              unsigned int color)
{
	 int x,y;
	 
	 for(x=x_s;x<x_e;x++)
	 {
		 for(y=y_s; y<y_e; y++)
		 {
		     lcd_draw_point(x,y,color);
		 
		 }
	 }
}

void lcd_draw_line(int x_s,int y_s, int x_e,int y_e,
              	  		 unsigned int color)
{
	if (x_s == x_e)
	{
		lcd_fill(x_s, y_s, x_e, y_e, color);
		return;
	}
	int bigger;
	int smaller;
	if (x_s > x_e)
	{
		bigger  = x_s;
		smaller = x_e;
	}
	else
	{
		bigger  = x_e;
		smaller = x_s;
	}

	int x = smaller;
	int y;
	for (; x <= bigger; ++x)
	{
		y = (x-x_s)*(y_e-y_s)/(x_e-x_s)+y_s;
		lcd_draw_point(x, y, color);
	}
}

void lcd_draw_bmp_horizontal(unsigned int x,unsigned int y,unsigned int picture_width,unsigned int picture_height,const unsigned char *picture_buf) 
{
    unsigned int blue, green, red;
    unsigned int color;
	unsigned int x_s = x;
	unsigned int x_e = x+picture_width;	
	unsigned int y_e = y+picture_height;
	

	for(;y < y_e; y++)
	{
		for (;x < x_e; x++)
		{
				/* 获取红绿蓝颜色数据 */
				blue  = *picture_buf++;
				green = *picture_buf++;
				red   = *picture_buf++;
				/* 组成24bit颜色 */
				color = red << 16 | green << 8 | blue << 0;
				lcd_draw_point(x, y, color);				
		}
		
		x = x_s;
	}

}

//动画衔接
void load_join(int msec)
{
	int i;
	char path[25];

	lcd_fill(0, 0, 800,480, 0x030303);

	for(i = 0;i < 50;i++)
	{
		sprintf(path, "./jpeg/load(%d).jpg", i+1);
		lcd_draw_jpg(250, 90, path, NULL, 0, 0);
		usleep(msec*1000);
	}
}

void show_open(int msec)
{
	int i;
	char path[25];
	for(i = 0;i < 70;i++)
	{
		sprintf(path, "./jpeg/open(%d).jpg", i+1);
		lcd_draw_jpg(0, 0, path, NULL, 0, 0);
		usleep(msec*1000);
	}
}
 
/* 左上角飞入 */
void oblique(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(i <= 800)
	{
		for(x=0; x<i; x++)
		{
			for(y=0; y<(i/5*3); y++)
			{
				color = ((480-(i/5*3)+y)*800+(800-i+x))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i+=10;
	}
}

//三角形四个方向移入
void pic_x(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0,
		j = 0;
	int color;
	
	while(i <= 400)
	{
		for(x=0; x<i; x++)
		{
			for(y=240-(i-x)*3/5; y<240+(i-x)*3/5; y++)
			{
				color = (y*800+400-(i-x))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = (y*800+400+(i-x))*3;
				lcd_draw_point(800-x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		for(y=0; y<j; y++)
		{
			for(x=400-(j-y)*5/3; x<400+(j-y)*5/3; x++)
			{
				color = ((240-(j-y))*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((240+(j-y))*800+x)*3;
				lcd_draw_point(x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		i+=10;
		j+=6;
		
	}
}


/*
	棱形
*/
void prismatic(const unsigned char *gImage_p)
{
	int x, y;
	int i = 400,
		j = 400;
	int color;
	
	while(1)
	{
		for(x=400; x<i; x++)
		{
			for(y=240-(i-x)/5*3; y<240+(i-x)/5*3; y++)
			{
				color = (y*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		for(x=400; x>j; x--)
		{
			for(y=240-(x-j)/5*3; y<240+(x-j)/5*3; y++)
			{
				color = (y*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		i+=10;
		j-=10;
		
		if(i>800)
			break;
	}
	
	i = 400,
	j = 400;
	
	while(1)
	{
		for(x=i; x<800; x++)
		{
			for(y=240-(400+i-x)/5*3; y<240+(400+i-x)/5*3; y++)
			{
				color = (y*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		for(x=j; x>0; x--)
		{
			for(y=240-(400+x-j)/5*3; y<240+(400+x-j)/5*3; y++)
			{
				color = (y*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		for(x=j; x<i; x++)
		{
			for(y=0; y<480; y++)
			{
				color = (y*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		i+=10;
		j-=10;
		
		if(i>800)
			break;
	}
	
	return;
}


/*
	分层
*/
void pic_storied(const unsigned char *gImage_p, const unsigned char *gImage_q)
{
	int a = 0;
	int i = 0,
		x, y;
	int color;
	
	while(1)
	{
		while(i < 440)
		{
			for(x=a; x<800; x++)
			{
				for(y=i; y<i+40; y++)
				{
					color = (y*800+x-a)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
			for(x=0; x<a; x++)
			{
				for(y=i; y<i+40; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_q[color+2],gImage_q[color+1],gImage_q[color]));
				}
			}
		
			for(x=0; x<800-a; x++)
			{
				for(y=i+40; y<i+80; y++)
				{
					color = (y*800+x+a)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
			for(x=800-a; x<800; x++)
			{
				for(y=i+40; y<i+80; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_q[color+2],gImage_q[color+1],gImage_q[color]));
				}
			}
			
			i+=80;
		}
		a+=20;
		i = 0;
		
		if(a > 800)
			break;
	}
	
	return;
}


//四角四方向移出
void pic_r_l_u_d(const unsigned char *gImage_p, const unsigned char *gImage_q)
{
	int x, y;
	int a = 400,
		b = 400,
		c = 240,
		d = 240;
	int color;
	
	while(1)
	{
		if(a >= 0)
		{
			for(x=0; x<a; x++)
			{
				for(y=0; y<240; y++)
				{
					color = (y*800+x+400-a)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
			for(x=a; x<400; x++)
			{
				for(y=0; y<240; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_q[color+2],gImage_q[color+1],gImage_q[color]));
				}
			}
		}
		if(b <= 800)
		{
			for(x=b; x<800; x++)
			{
				for(y=240; y<480; y++)
				{
					color = (y*800+400+x-b)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
			for(x=400; x<b; x++)
			{
				for(y=240; y<480; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_q[color+2],gImage_q[color+1],gImage_q[color]));
				}
			}
		}
		if(c >= 0)
		{
			for(x=400; x<800; x++)
			{
				for(y=0; y<c; y++)
				{
					color = ((240-c+y)*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
			for(x=400; x<800; x++)
			{
				for(y=c; y<240; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_q[color+2],gImage_q[color+1],gImage_q[color]));
				}
			}
		}
		if(d <= 480)
		{
			for(x=0; x<400; x++)
			{
				for(y=d; y<480; y++)
				{
					color = ((y-d+240)*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
			for(x=0; x<400; x++)
			{
				for(y=240; y<d; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_q[color+2],gImage_q[color+1],gImage_q[color]));
				}
			}
		}
		
		a-=10;
		b+=10;
		c-=6;
		d+=6;
		
		if(a < 0)
			break;
	}
	
	
	return;
}

/* --> <-- */
void pic_rltoc(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0,
		j = 799;
	int color;
	
	while(1)
	{
		for(x=0; x<i; x++)
		{
			for(y=0; y<480; y++)
			{
				color = (y*800+(400-i+x))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		for(x=800; x>j; x--)
		{
			for(y=0; y<480; y++)
			{
				color = (y*800+(x-j+400))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		j-=10;
		i+=10;
		
		if(i > 400)
			break;
	}
	
	return;
	
}
/* --> */
void pic_ltor(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(1)
	{
		for(x=0; x<i; x++)
		{
			for(y=0; y<480; y++)
			{
				color = (y*800+(800-i+x))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i+=40;
		
		if(i > 800)
			break;
	}
	
	return;
	
}

/* <-- */
void pic_rtol(const unsigned char *gImage_p)
{
	int x, y;
	int i = 800;
	int color;
	
	while(1)
	{
		for(x=800; x>=i; x--)
		{
			for(y=0; y<480; y++)
			{
				color = (y*800+(x-i))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i-=40;
		
		if(i < 0)
			break;
	}
	
	return;
	
}
 
/* 		 |
		 |
		\ /
 */
void pic_utod(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(1)
	{
		for(y=0; y<i; y++)
		{
			for(x=0; x<800; x++)
			{
				color = ((480-i+y)*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i+=20;
		
		if(i > 480)
			break;
	}
	
}

/*		/ \
		 |
		 |
 */
void pic_dtou(const unsigned char *gImage_p)
{
	int x, y;
	int i = 480;
	int color;
	
	while(1)
	{
		for(y=480; y>=i; y--)
		{
			for(x=0; x<800; x++)
			{
				color = ((y-i)*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i-=20;
		
		if(i < 0)
			break;
	}
	
}
//锯齿形
void pic_test4(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(i <= 480)
	{
		for(y=0; y<=i; y++)
		{
			for(x=0; x<(i-y)*5/12; x++)
			{
				color = ((480-i+y)*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((480-i+y)*800+x+400)*3;
				lcd_draw_point(x+400,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
			for(x=400-(i-y)*5/12; x<400; x++)
			{
				color = ((480-i+y)*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((480-i+y)*800+x+400)*3;
				lcd_draw_point(x+400,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
			for(x=200-(i-y)*5/12; x<200+(i-y)*5/12; x++)
			{
				color = ((i-y)*800+x)*3;
				lcd_draw_point(x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((i-y)*800+x+400)*3;
				lcd_draw_point(x+400,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		
		i+=10;
	}
	
	
	
	return;
}

//左右200移入，中间上下移入
void pic_test3(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(1)
	{
		for(x=0; x<=i; x++)
		{
			for(y=0; y<480; y++)
			{
				color = (y*800+200-i+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = (y*800+i-x+600)*3;
				lcd_draw_point(800-x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i+=10;
		
		if(i > 200)
			break;
	}
	
	
	i = 0;
	while(1)
	{
		for(x=200; x<600; x++)
		{
			for(y=0; y<=i; y++)
			{
				color = ((240-i+y)*800+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((i-y+240)*800+x)*3;
				lcd_draw_point(x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i+=10;
		
		if(i > 240)
			break;
	}
	
	
	
	return;
}

//四角飞入
void pic_test2(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(i <= 400)
	{
		for(x=0; x<i; x++)
		{
			for(y=0; y<(i/5*3); y++)
			{
				color = ((240-(i/5*3)+y)*800+(400-i+x))*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((240+(i/5*3)-y)*800+(400-i+x))*3;
				lcd_draw_point(x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((240-(i/5*3)+y)*800+(400+i-x))*3;
				lcd_draw_point(800-x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((240+(i/5*3)-y)*800+(400+i-x))*3;
				lcd_draw_point(800-x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
			
		i+=5;
	}
	
	
	return;
	
}

//上下分屏，左右移入
void pic_test1(const unsigned char *gImage_p)
{
	int x, y;
	int i = 0;
	int color;
	
	while(1)
	{
		for(x=0; x<i; x++)
		{
			for(y=0; y<240; y++)
			{
				color = (y*800+800-i+x)*3;
				lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				color = ((y+240)*800+i-x)*3;
				lcd_draw_point(800-x,y+240,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
			}
		}
		
		i+=20;
		
		if(i > 800)
			break;
	}
}
void rount(const unsigned char *gImage_p)
{
	int x, y,num1,num2;
	int i = 400;
	int color;
	
	while(i < 880)
	{
		/*if(i <= 800)
		{
			for(x=400-(i-400); x<i; x++)
			{
				num1 = x>400? x-400 : 400-x;
				num1 = sqrt((i-400)*(i-400)-(num1*num1));
				
				num2 = num1>240? 240 : num1;
				
				for(y=240-num2; y<240+num2; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
		}
		else
		{
			for(x=0; x<800; x++)
			{
				num1 = x>400? x-400 : 400-x;
				num1 = sqrt((i-400)*(i-400)-(num1*num1));
				
				num2 = num1>240? 240 : num1;
				
				for(y=240-num2; y<240+num2; y++)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
		}*/
		
		for(x=0; x<800; x++)
		{
			for(y=0; y<480; y++)
			{
				num1 = (x-400)*(x-400)+(y-240)*(y-240);
				num2 = (i-400)*(i-400);
				if(num1<=num2)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
					
				}
			}
		}
		
		i+=10;
	}
	
	return;
}

void rount_4(const unsigned char *gImage_p)
{
	int x, y,num1, num2;
	int i = 0;
	int color;
	
	while(i < 480)
	{
		for(x=0; x<=400; x++)
		{
			for(y=0; y<=240; y++)
			{
				num1 = x*x+y*y;
				num2 = i*i;
				if(num1<=num2)
				{
					color = (y*800+x)*3;
					lcd_draw_point(x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
					color = (y*800+800-x)*3;
					lcd_draw_point(800-x,y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
					color = ((480-y)*800+x)*3;
					lcd_draw_point(x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
					color = ((480-y)*800+800-x)*3;
					lcd_draw_point(800-x,480-y,RGB(gImage_p[color+2],gImage_p[color+1],gImage_p[color]));
				}
			}
		}
		
		i+=10;
	}
	
	
	return;
}